﻿using HeyPiPi_Desk.Domain;
using LibrarySystem.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace HeyPiPi_Desk.Tools
{

    /// <summary>
    /// 打开的面板是重新生成还是使用已经存在的
    /// </summary>
    public enum DomainStatus{
        newdomain=0, //打开新的面板
        keepdomain=1, // 使用已经存在的面板

    }

    //容器挑战的方式
    public enum DomainOpenMethod
    {
        TreeOpen,//树形图打开
        PageOpen,//页面打开
        HistoryOpen,//历史打开
    }

    /// <summary>
    /// 面板容器
    /// </summary>
    public class DomainResponsitory
    {
      public delegate void TogglePanelDelegate(string name , DomainOpenMethod type);

        public event TogglePanelDelegate TogglePanelEvent;

        /// <summary>
        /// 面板打开方式
        /// </summary>
        public DomainOpenMethod OpenMethod { get; set; } = DomainOpenMethod.TreeOpen;

        public void ChangedPanel(string name, DomainOpenMethod type)
        {
            if (TogglePanelEvent!=null)
            {
                this.OpenMethod = type;
                this.TogglePanelEvent(name,type);
            }
        }

       

        private static DomainResponsitory _instance = null;

        private Dictionary<string, UserControl> _domainContainer = new Dictionary<string, UserControl>();

        /// <summary>
        /// 撤销的栈
        /// </summary>
        private Stack<string> _inStack = new Stack<string>(Convert.ToInt32(GlobalSetting.GetGlobalSetting().GetSetting(SettingParamEnum.HistoryCount)));
        /// <summary>
        /// 反撤销的栈
        /// </summary>
        private Stack<string> _outStack = new Stack<string>(Convert.ToInt32(GlobalSetting.GetGlobalSetting().GetSetting(SettingParamEnum.HistoryCount)));


        private DomainResponsitory()
        {
            this._domainContainer.Add("主页", new Home());
            this._domainContainer.Add("快速收银", new QuickPayView());
            this._domainContainer.Add("自定义收银", new CustomPayView()); 
            this._domainContainer.Add("挂单", new WaitPayView()); 
            this._domainContainer.Add("会员列表", new VIPListView());
            this._domainContainer.Add("会员办卡", new VIPBuyCardView());
            this._domainContainer.Add("会员等级管理", new VIPLevelManagerView());
            this._domainContainer.Add("礼品管理", new GiftManagerView());
            this._domainContainer.Add("积分记录", new PointsRecordView());
            this._domainContainer.Add("还款记录", new RepaymentView());
            this._domainContainer.Add("服务项目管理", new ServiceItemManagerView());
            this._domainContainer.Add("服务分类管理", new ServiceClassManagerView());
            this._domainContainer.Add("次卡列表", new CountCardListView());
            this._domainContainer.Add("已售次卡", new SellCountCardView());
            this._domainContainer.Add("商品列表", new ProductListView());
            this._domainContainer.Add("商品分类", new productClassListView());
            this._domainContainer.Add("员工列表", new StaffListView());
            this._domainContainer.Add("职位管理", new JobManagerView());
            this._domainContainer.Add("服务提成设置", new ServiceDeductSettingView());
            this._domainContainer.Add("商品销售设置", new ProductDeductView());
            this._domainContainer.Add("开卡/续费设置", new CardReSellDeductView());
            this._domainContainer.Add("次卡办理设置", new CountCardBuildDeductView());
            this._domainContainer.Add("次卡消费设置", new CountCardSellDeductView());
            this._domainContainer.Add("客户消费明细", new ClientBuyDetailView());
            this._domainContainer.Add("店铺财务总表", new ShopMoneyTableView());
            this._domainContainer.Add("商品销售报表", new GoodsSellTableView());
            this._domainContainer.Add("会员办卡报表", new VipCardTableView());
            this._domainContainer.Add("会员续费报表", new VipReBuyTableView());

        }
        public static DomainResponsitory GetInstance()
        {
            if (_instance==null)
            {
                _instance = new DomainResponsitory();
           
            }
            return _instance;
        }

        /// <summary>
        /// 根据名称获得指定的面板，传入的枚举表示是否创建新的面板
        /// </summary>
        /// <param name="name">面板名称</param>
        /// <param name="status">是否保持</param>
        /// <returns>返回指定面板</returns>
        public UserControl GetDomain(string name, DomainStatus status = DomainStatus.keepdomain)
        {
            if (status==DomainStatus.keepdomain)
            {
              return   this.KeepDomain(name);
            }
            else
            {
             return   this.NewDomain(name);
            }
        }



        private UserControl KeepDomain(string name)
        {
            if (this._domainContainer.ContainsKey(name))
            {
                return this._domainContainer[name];
            }
            else
            {
                return null;
            }
            
        }

        private UserControl NewDomain(string name)
        {
            UserControl userControl = null;
            switch (name)
            {
                case "主页":
                    userControl = new Home();
                    break;
                case "快速收银":
                    userControl = new QuickPayView();
                    break;
                case "自定义收银":
                    userControl = new CustomPayView();
                    break;
                case "挂单":
                    userControl = new WaitPayView();
                    break;
                case "会员列表":
                    userControl = new VIPListView();
                    break;
                case "会员办卡":
                    userControl = new VIPBuyCardView();
                    break;
                case "会员等级管理":
                    userControl = new VIPLevelManagerView();
                    break;
                case "礼品管理":
                    userControl = new GiftManagerView();
                    break;
                case "积分记录":
                    userControl = new PointsRecordView();
                    break;
                case "还款记录":
                    userControl = new RepaymentView();
                    break;
                case "服务项目管理":
                    userControl = new ServiceItemManagerView();
                    break;
                case "服务分类管理":
                    userControl = new ServiceClassManagerView();
                    break;
                case "次卡列表":
                    userControl = new CountCardListView();
                    break;
                case "已售次卡":
                    userControl = new SellCountCardView();
                    break;
                case "商品列表":
                    userControl = new ProductListView();
                    break;
                case "商品分类":
                    userControl = new productClassListView();
                    break;
                case "员工列表":
                    userControl = new StaffListView();
                    break;
                case "职位管理":
                    userControl = new JobManagerView();
                    break;
                case "服务提成设置":
                    userControl = new ServiceDeductSettingView();
                    break;
                case "商品销售设置":
                    userControl = new ProductDeductView();
                    break;
                case "开卡/续费设置":
                    userControl = new CardReSellDeductView();
                    break;
                case "次卡办理设置":
                    userControl = new CountCardBuildDeductView();
                    break;
                case "次卡消费设置":
                    userControl = new CountCardSellDeductView();
                    break;
                case "客户消费明细":
                    userControl = new ClientBuyDetailView();
                    break;
                case "店铺财务总表":
                    userControl = new ShopMoneyTableView();
                    break;
                case "项目销售报表":
                    userControl = new ProjectSellTableView();
                    break;
                case "商品销售报表":
                    userControl = new GoodsSellTableView();
                    break;
                case "会员办卡报表":
                    userControl = new VipCardTableView();
                    break;
                case "会员续费报表":
                    userControl = new VipReBuyTableView();
                    break;

                default:
                    break; ;
            }
            if (userControl==null)
            {
                return null;
            }
            this._domainContainer[name] = userControl;
            return userControl;

        }


        /// <summary>
        /// 压入新的面板名称到栈中
        /// </summary>
        /// <param name="name"></param>
        public void PushItemInStack(string name)
        {
            this._inStack.Push(name);

        }

        /// <summary>
        /// 撤销
        /// </summary>
        /// <returns></returns>
        public string Redo()
        {
            if (this._inStack.Count==0)
            {
                return null;
            }

            string name = this._inStack.Pop();

            if (this._outStack.Count!=0)
            {
                if (!string.IsNullOrEmpty(name) && name != this._outStack.Peek())
                {
                    this._outStack.Push(name);
                }
            }
            else
            {
                this._outStack.Push(name);
            }

         
            return name;
        }
        /// <summary>
        ///  反撤销
        /// </summary>
        /// <returns></returns>

        public string UnRedo()
        {
            if (this._outStack.Count==0)
            {
                return null;
            }
            string name = this._outStack.Pop();
            if (this._inStack.Count!=0)
            {
                if (!string.IsNullOrWhiteSpace(name) && name != this._inStack.Peek())
                {

                    this._inStack.Push(name);
                }
            }
            else
            {
                this._inStack.Push(name);
            }


        
            return name;
        }

        /// <summary>
        /// 清除面板历史
        /// </summary>
        /// <returns></returns>
        public bool ClearHistory()
        {
            try
            {
                this._inStack.Clear();
                this._outStack.Clear();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }


        

    }
}
